import { ResponseInterceptor } from './../../../interceptor/response/response.interceptor';
import config from 'src/config/config';
import { Body, Controller, Delete, Get, Patch, Post, UseInterceptors } from '@nestjs/common';
import { ShopSettingService } from 'src/service/shop-setting/shop-setting.service';

@Controller(`${config.adminPath}/shopSetting`)
@UseInterceptors(ResponseInterceptor)
export class ShopSettingController {
    constructor(private readonly shopSettingService: ShopSettingService) {}
    @Get()
    async find() {
        return await this.shopSettingService.getModel().aggregate([
            {
                $lookup: {
                    localField: 'main_cate_list',
                    foreignField: '_id',
                    as: 'main_cate_list',
                    from: 'goods_cate',
                },
            },
        ]);
    }
    @Post()
    async create(@Body() body) {
        //只允许数据库有一条数据，该接口既能编辑又能新增
        const shopSettingResult = await this.shopSettingService.find();
        if (shopSettingResult && shopSettingResult.length) {
            return await this.shopSettingService.update(body);
        }
        return await this.shopSettingService.create(body);
    }
    @Patch()
    async update(@Body() body) {
        return await this.shopSettingService.update(body);
    }
    @Delete()
    async delete(@Body() body) {
        return await this.shopSettingService.delete(body);
    }
}
